<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-form :inline="true">

        <el-form-item label="名称">
          <el-input v-model="listQuery.name" clearable class="filter-item" style="width: 180px;" placeholder="名称"/>
        </el-form-item>

        <el-form-item label="联系人">
          <el-input v-model="listQuery.contactPerson" clearable class="filter-item" style="width: 180px;" placeholder="联系人"/>
        </el-form-item>

        <el-form-item label="联系电话">
          <el-input v-model="listQuery.phoneNumber" clearable class="filter-item" style="width: 180px;" placeholder="联系电话"/>
        </el-form-item>

        <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>

        <el-button
          v-hasPermi="['admin:organization:create']"
          class="filter-item"
          style="float:right"
          type="primary"
          icon="el-icon-plus"
          @click="startAdd">添加</el-button>
      </el-form>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row >

      <el-table-column align="center" label="组织名称" prop="name" />

      <el-table-column align="center" label="联系人" prop="contactPerson"/>

      <el-table-column align="center" label="联系电话" prop="phoneNumber"/>

      <el-table-column align="center" label="创建时间" prop="createTime"/>

      <el-table-column align="center" label="操作" width="250">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['admin:organization:update']"
            type="primary"
            size="small"
            @click="startEditDepartment(scope.row)">编辑</el-button>
         <!-- <el-button
            v-hasPermi="['admin:organization:delete']"
            type="primary"
            size="small"
            @click="deleteDepartment(scope.row.name, scope.row.id)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.pageSize" @pagination="getList" />

    <el-dialog :close-on-click-modal="false" :title="dialogType == 1?'添加组织':'修改组织'" :visible.sync="dialogFormVisible" width="28%">
      <el-form ref="dialogForm" :rules="formRules" :model="dialogForm" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label=" 组织名称  " prop="name" style="width: 100%;">
              <el-input v-model="dialogForm.name" clearable class="filter-item" style="width: 100%;"  placeholder=""/>
            </el-form-item>
          </el-col>
        </el-row>
				<el-form-item label=" 机构名称  " prop="departmentId">
				  <el-select :disabled="dialogType != 1" v-model="dialogForm.departmentId" placeholder="请选择" style="width:100%;" >
				    <el-option
				      v-for="item in departmentList"
				      :key="item.id"
				      :label="item.name"
				      :value="item.id"/>
				  </el-select>
				</el-form-item>
        <el-form-item label="  联系人 " prop="contactPerson" style="width: 100%;">
          <el-input v-model="dialogForm.contactPerson" clearable class="filter-item" style="width: 100%;" placeholder=""/>
        </el-form-item>
        <el-form-item label="  联系电话  " prop="phoneNumber">
          <el-input v-model="dialogForm.phoneNumber" onkeyup="value=value.replace(/[^\d-]/g,'')" style="width: 100%;" clearable class="filter-item"  placeholder=""/>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer" align="center">
        <el-button @click="confirmEdit('dialogForm')">确定</el-button>
        <el-button @click="dialogFormVisible = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { fetchList, organizationDetail, createOrganization, deleteOrganization,
updateOrganization } from '@/api/system/organization'
import { getDepartmentList } from '@/api/base'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'Organization',
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        pageSize: 20
      },
      downloadLoading: false,
      dialogFormVisible: false,
      dialogForm: {},
      dialogType: 1,
      name: '',
      deleteDepartmentId: 0,
      formRules: {
        name: [{ required: true, message: '请输入组织名称', trigger: 'blur' }],
        contactPerson: [{ required: true, message: '请输入联系人', trigger: 'blur' }],
        phoneNumber: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
      },
			departmentList: [],//机构数据
    }
  },
  created() {
    getDepartmentList().then(res => {
		  this.departmentList = res.data || []
		  this.getList()
		})
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        let data = response.data || {}
        this.list = data.list || []
        this.total = data.total || 0
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    // getStatus(status) {
    //   if (status === 1) {
    //     return '启用'
    //   }
    //   return '停用'
    // },
    deleteDepartment(name, departmentId) {
      this.$confirm(`您确定删除组织：${name}`, '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteOrganization({ 'id': departmentId }).then(response => {
          this.$message({
            message: '删除成功!',
            type: 'success'
          })
          this.getList()
        })
      })
    },
    startAdd() {
      this.dialogFormVisible = true
      this.dialogType = 1
      this.dialogForm = {}
      this.$nextTick(() => {
        this.$refs.dialogForm.clearValidate()
      })
    },
    confirmEdit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.dialogType === 1) {
            createOrganization(this.dialogForm).then(response => {
              this.$message({
                message: '添加成功!',
                type: 'success'
              })
              this.getList()
              this.dialogFormVisible = false
              this.dialogForm = {}
            })
          } else {
            updateOrganization(this.dialogForm).then(response => {
              this.$message({
                message: '修改成功!',
                type: 'success'
              })
              this.getList()
              this.dialogFormVisible = false
              this.dialogForm = {}
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    startEditDepartment(row) {
      organizationDetail(row.id).then(response => {
        this.dialogFormVisible = true
        this.dialogType = 2
        this.dialogForm = response.data || {}
        this.$nextTick(() => {
          this.$refs.dialogForm.clearValidate()
        })
      })

    }

  }
}
</script>
<style scoped>
  .blue{
    color: blue;
  }
  .qrcode{
    margin: 0 auto;
    width: 250px;
  }
</style>
